Plotly

What is Plotly

How can you use Plotly

Plotly for Python - Installation

To install Plotly's Python package, use the package manager pip inside your terminal.

pip install plotly

or

sudo pip install plotly

Initialize by writing

import plotly

The Hello World Example for Offline Use

import plotly as py
py.offline.init_notebook_mode() # run at the start of every notebook
py.offline.iplot({
"data": [{
    "x": [1, 2, 3],
    "y": [4, 2, 5]
}],ß
"layout": {
    "title": "hello world"
}
})

Example - Bubble Charts

Bubble charts can be used for ....

In [17]:
import plotly as py
import plotly.graph_objs as go
py.offline.init_notebook_mode() 

trace0 = go.Scatter(
    x=[1, 2, 3, 4],
    y=[10, 11, 12, 13],
    mode='markers',
    marker=dict(
        size=[40, 60, 80, 100],
    )
)
data = [trace0]
layout = go.Layout(
    showlegend=False,
    height=600,
    width=600,
)
fig = go.Figure(data=data, layout=layout)
py.offline.plot(fig, filename='buuble_chart.html')
Out[17]:
'file:///Users/svenballentin/Desktop/Private/Data_Visualization/workshop_interactive_viz/my-graph.html'
In [25]:
# Setting marker size, color, opacity and add a legend
import plotly as py
import plotly.graph_objs as go

data=[
    {
        'x':[1, 2, 3, 4],
        'y':[10, 11, 12, 13],
        'mode':'markers',
        'marker':{
            'color': [120, 125, 130, 135, 140],
            'opacity':[1, 0.8, 0.6, 0.4],
            'size':[40, 60, 80, 100],
            'showscale':True
        }
    }
]

layout = go.Layout(
    showlegend=False,
    height=600,
    width=600,
)
fig = go.Figure(data=data, layout=layout)
py.offline.plot(fig, filename='bubble_chart.html')
Out[25]:
'file:///Users/svenballentin/Desktop/Private/Data_Visualization/workshop_interactive_viz/bubble_chart.html'
In [ ]:
# Hover text with bubble charts
import plotly as py
import plotly.graph_objs as go

data=[
    {
        'x':[1, 2, 3, 4],
        'y':[10, 11, 12, 13],
        'text':['size: 40', 'size: 60', 'size: 80', 'size: 100'],
        'mode':'markers',
        'marker':{
            'color': [120, 125, 130, 135, 140],
            'opacity':[1, 0.8, 0.6, 0.4],
            'size':[40, 60, 80, 100],
            'showscale':True
        }
    }
]

data = [trace0]
layout = go.Layout(
    showlegend=False,
    height=600,
    width=600,
)
fig = go.Figure(data=data, layout=layout)
py.offline.plot(fig, filename='bubble_chart.html')